package cn.framelife.rainbow.db.creator;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


import cn.framelife.rainbow.db.creator.part.ActionCreator;
import cn.framelife.rainbow.db.creator.part.ConfigFileCreator;
import cn.framelife.rainbow.db.creator.part.DaoCreator;
import cn.framelife.rainbow.db.creator.part.EntityCreator;
import cn.framelife.rainbow.db.creator.part.MenuCreator;
import cn.framelife.rainbow.db.creator.part.PageCreator;
import cn.framelife.rainbow.db.creator.part.ServiceCreator;
import cn.framelife.rainbow.db.entity.ClazzProperty;
import cn.framelife.rainbow.db.entity.Menu;
import cn.framelife.rainbow.db.entity.MenuList;
import cn.framelife.rainbow.ui.entity.Module;
import cn.framelife.rainbow.ui.entity.ReverseProperty;
import cn.framelife.rainbow.util.FileUtil;
import cn.framelife.rainbow.util.StringUtil;

/**
 * 数据表转代码生成实体类
 * 
 * @author 潘广伟 p_3er@qq.com  
 * @date 2014-1-7 下午4:43:40
 */
public abstract class CodeCreator implements ICodeCreator {
	
	protected ReverseProperty reverseProperty;
	protected String dialect;
	protected EntityCreator entityCreator;

	public void createCode(List<Module> modules)
			throws  Exception {
		String projectName = reverseProperty.getSelectionProjectName();
		String mainPackage = reverseProperty.getPackageURL();
		List<String> annotatedClasses = new ArrayList<String>();
		Set<String> actionPackages = new HashSet<String>();
		MenuList menus = new MenuList();
		
		
		for(Module module : modules){
			String packageURL = StringUtil.merge(mainPackage,".",module.getModuleName());
			
			List<ClazzProperty> entities = entityCreator.createEntities(module.getTableNames(),packageURL);
			
			DaoCreator daoCreator = new DaoCreator(projectName, packageURL);
			ServiceCreator serviceCreator = new ServiceCreator(projectName, packageURL);
			ActionCreator actionCreator = new ActionCreator(projectName, packageURL);
			PageCreator pageCreator = new PageCreator(entityCreator);
			
			Menu parantMenu = new Menu();
			parantMenu.setId(module.getModuleName());
			parantMenu.setText(module.getMenuName());
			parantMenu.setLeaf(false);
			menus.add(parantMenu);
			
			for(ClazzProperty entity : entities){
				/**
				 * Entity相关
				 */
				if(reverseProperty.isPOJO()){
					entityCreator.createCodeForEntity(entity);
					annotatedClasses.add(StringUtil.merge(entity.getPackageURL(),".",entity.getClassName()));
				}
				
				/**
				 * Dao相关
				 */
				if(reverseProperty.isDAO()){
					/**
					 * 创建与实体类相关的Dao
					 */
					daoCreator.createCodeForEntityDao(entity);
				}
				
				/**
				 * 创建与实体类相关的Service
				 */
				if(reverseProperty.isService()){
					serviceCreator.createCodeForService(entity);		
				}
				
				/**
				 * 创建与实体类相关的Action
				 */
				if(reverseProperty.isAction()){
					actionCreator.createCodeForAction(entity);
					actionPackages.add(StringUtil.merge(packageURL,".struts"));
				}
				
				/**
				 * 生成页面
				 */
				if(reverseProperty.isPage()){
					/**
					 * 生成页面
					 */
					pageCreator.createCodeForPage(module,entity);
					
					Menu menu = new Menu();
					menu.setId(entity.getClassName());
					menu.setText(entity.getClassComment());
					menu.setParentId(module.getModuleName());
					menu.setParentText(module.getMenuName());
					
					menu.setLeaf(true);
					menus.add(menu);
				}			
			}		
		}	
		
		/**
		 * 配置文件
		 */
		if(reverseProperty.isUpdateConfigFile()){
			ConfigFileCreator configFileCreator = new ConfigFileCreator(projectName, mainPackage);
			/**
			 * Spring配置文件
			 * 判断配置文件是否存在，如果存在，则只更新实体类的路径
			 */
			configFileCreator.createCodeForSpring(dialect, annotatedClasses);
				
			/**
			 * web.xml配置文件
			 * 后期判断是否有某一节点，有则更新，无则新建
			 */
			configFileCreator.createCodeForWeb(actionPackages);
		}
		
		/**
		 * 生成页面
		 */
		if(reverseProperty.isPage()){
			/**
			 * 拷贝必须文件
			 */
			FileUtil.copyPageCommon(projectName);
		}
		
		/**
		 * 创建MenuData.json及MenuAction
		 */
		if(reverseProperty.isMenuAction()){
			MenuCreator menuCreator = new MenuCreator(projectName, mainPackage);
			menuCreator.createrCodeForMenu(menus);
		}
		
		/**
		 * 创建与实体类相关的Service
		 */
		if(reverseProperty.isService()){
			/**
			 * 拷贝工具类
			 */
			FileUtil.copyUtilClass(projectName);
			FileUtil.copyBaseDaoClass(projectName);
		}
		
		/**
		 * 加入JARs
		 */
		if(reverseProperty.isJARs()){
			FileUtil.copyJARsToProject(projectName);
		}
	}
}
